from lwpgt.gui import Widget
import pygame
import lwpgt

class ListCtrl(Widget):
    """List Control Widget for lwpgt"""
    
    def __init__(self,parent,  pos=[0,0],size=[150,100], textsize=18, unselectable=True):
        """init widget
        arguments:
        parent - 
        position - position of the widget 
        size - size of the widget
        textsize - text height
        unselectable - default True 
        """
        Widget.__init__(self, parent, pos, size)
        self.textsize=textsize
        self.unselectable=unselectable
        #font 
        self.font=pygame.font.SysFont("monospace",self.textsize)
        #list data
        self.listdata=[]
        self.listposition=0
        self.selected=None
        #callbacks
        self.onListSelect=None
        #THEME
        self.theme={
            "BACKGROUND_COLOR":(70, 70, 128), 
            "SELECTED_TEXT_COLOR":(0, 0, 255), 
            "UNSELECTED_TEXT_COLOR":(240, 240, 240), 
            "FRAME_COLOR":(255, 255, 255), 
            }
        
    def Event(self, event):
        """Event handler for widget"""
        mx,my=pygame.mouse.get_pos()
        #changeing active status
        if self.IsUnder((mx,my)) and event.type==pygame.MOUSEBUTTONDOWN:
            self.active=True
        elif event.type==pygame.MOUSEBUTTONDOWN:
            self.active=False
        #select item when left mouse button was been pressed
        if self.active==True and event.type==pygame.MOUSEBUTTONDOWN:
            #only left mouse button can select item
            if pygame.mouse.get_pressed()[0]==1:
                #set selectes
                tempselected=self.selected
                self.selected=(my-self.pos[1])/self.textsize+self.listposition
                #update selected
                if self.selected>len(self.listdata)-1:
                    #set selected to None if widget is unselectable
                    if self.unselectable:self.selected=None
                    else:self.selected=tempselected
                else:
                    if self.onListSelect!=None:
                        self.onListSelect(self)
                        
            #scroll up
            if event.button==4:
                self.listposition-=1
                if self.listposition<0:self.listposition=0
            #scroll down
            if event.button==5:
                self.listposition+=1
                if self.listposition>len(self.listdata)-1:self.listposition=len(self.listdata)-1
    
    def Draw(self, surface):
        """draw widget"""
        img=pygame.Surface(self.size)
        img.set_colorkey((255, 0, 255))
        img.fill(self.theme["BACKGROUND_COLOR"])
        
        #DRAW LIST
        linestodraw=0
        color=()
        #calculate number of entries to draw
        nroflines=len(self.listdata)-self.listposition
        widgetlines=self.size[1]/self.textsize
        if nroflines<=widgetlines:
            linestodraw=nroflines
        elif nroflines>widgetlines:
            linestodraw=widgetlines
        #draw entries
        for line in range(linestodraw):
            if line+self.listposition==self.selected:
                color=self.theme["SELECTED_TEXT_COLOR"]
                self.font.set_bold(True)
            else:
                self.font.set_bold(False)
                color=self.theme["UNSELECTED_TEXT_COLOR"]
            lineimg=self.font.render(self.listdata[self.listposition+line], 0, color)
            img.blit(lineimg, (self.size[0]/2-lineimg.get_size()[0]/2, self.textsize*line+3))
        #rect
        pygame.draw.rect(img, self.theme["FRAME_COLOR"], (0,0, self.size[0], self.size[1]), 1)
        #blit widget to screen
        surface.blit(img, self.pos)
    
    def IsSelected(self):
        """check list selection flag return true/false"""
        if self.selected!=None:
            return True
        else:return False 
    
    def GetSelected(self):
        """get list selection"""
        return self.selected
    
    def Select(self, index):
        """select list entry"""
        if index>len(self.listdata)-1:
            index=len(self.listdata)-1
        self.selected=index
    
    def SetList(self,lists):
        """Set ListCtrl entries to show """
        self.listdata=lists
    
    def GetList(self):
        """get entries from ListCtrl"""
        return self.listdata
    
    def Connect(self, signal, function):
        """callbacks handler"""
        if signal==lwpgt.EVT_LISTSELECT:self.onListSelect=function
